#!/usr/bin/env node

/*
 * 服务程序退出时将待办事项数组保存到当前目录的 data.txt 中
 * 服务程序启动时将待办事项从 data.txt 中读取到 items 数组中
*/
const http=require('http'),
      fs=require('fs'),
      log=console.log,
      ITEMS=loadData();

http.createServer((req,res)=>{
  log(req.headers);
  log('');
  switch(req.method){
    case 'GET':
      get(res);
      break;
    case 'POST':
      insert(req,res);
      break;
    case 'DELETE':
      del(req,res);
      break;
    case 'PUT':
      change(req,res);
      break;
    default:
      break;
  }
}).listen(8080);

function get(res){
  let body=JSON.stringify(items);
  res.setHeader('Content-Length',Buffer.byteLength(body));
  res.setHeader('Content-Type','text/plain;charset="utf-8"');
  res.setHeader('Access-Control-Allow-Origin','*');
  res.end(body);
}
function insert(req,res){
  let item='';

  req.on('data',data=>{item+=data;});
  req.on('end',()=>{
    items.push(item);
    res.setHeader('Access-Control-Allow-Origin','*');
    res.end('Insert ok!');
  });
}

function del(req,res){
  let arg=req.url.split('/');
  if(arg[1]===''){
    items=[];
  }
  res.setHeader('Access-Control-Allow-Orgin','*');
  if(!items[i]){
    res.statusCode=404;
    res.end('Not found');
  }else{
    items.splice(i,1);
    res.end('Delete ok!');
  }
}

function change(req,res){
  let arg=req.url.split('/');
  if(arg[1] === '') {
    items = [];      
  }

  let item = '';
  res.setHeader('Access-Control-Allow-Origin', '*');
  req.on('data', (chunk) => { item += chunk;  });
  req.on('end', () => {
    let i = parseInt(arg[1]);
    if(!items[i]) {
      res.statusCode = 404;
      res.end('Not found');                          
    } else {
      items[i] = item;
      res.end('Change OK');                          
    }            
  });
}

function loadData(){
  try{
    let data=fs.readFileSync('./data.txt','utf8');
    log(data);
    return JSON.parse(data);
  }catch(e){
    return [];
  }
}

process.on('SIGINT',()=>{
  fs.writeFileSync('./data.txt',JSON.stringify(items));
  process.exit();
});




